<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

require_once "_compatibility/MagicQuotes.php";
require_once "_util/ClassPath.php";
require_once "Zend/Loader/Autoloader.php";
// Set up autoload.
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initRestRoute()
	{
		$this->bootstrap('frontController');
		$frontController = Zend_Controller_Front::getInstance();
		$restRoute = new Zend_Rest_Route($frontController);
		$frontController->getRouter()->addRoute('default', $restRoute);
		
	}

	protected function _initDB()
	{
		/*
		 * local database which is sandboxed and restricted
		 */
    	$config = new Zend_Config_Ini(dirname(__FILE__) . '/configs/mysql.ini' , 'mysql'); 
    	
        $db = Zend_Db::factory("Pdo_Mysql", array(
								'host' => $config->db->host,
								'username' => $config->db->user,
								'password' => $config->db->password,
								'dbname' => $config->db->basename,
    							));

    	Zend_Db_Table::setDefaultAdapter($db);
    	
	}
	
	protected function _initSettings()
	{
    	$config = new Zend_Config_Ini(dirname(__FILE__) . '/configs/settings.ini' , 'settings'); 
		define('ENCRYPT_KEY', $config->encryption->key);
		
		$frontController = Zend_Controller_Front::getInstance();
		
		if ($config->application->debug == true):
			define(DEBUG, true);
			ini_set('display_errors', 'on');
			Zend_Loader_Autoloader::getInstance()->suppressNotFoundWarnings(false);
			$frontController->throwExceptions(true);
			$frontController->setParam('displayExceptions', true);
			$frontController->setParam('noErrorHandler', false);
		else:
			define(DEBUG, false);
			Zend_Loader_Autoloader::getInstance()->suppressNotFoundWarnings(true);
			$frontController->throwExceptions(false);
			$frontController->setParam('displayExceptions', false);
			$frontController->setParam('noErrorHandler', true);
		endif;
		
		Zend_Registry::set('identification.partnerid', $config->identification->partnerid);
		Zend_Registry::set('identification.partnername', $config->identification->partnername);
		
		Zend_Registry::set('partner.authentication', $config->partner->authentication);
		
		Zend_Registry::set('application.version', $config->application->version);
	}
	
	public function _initSessionAfterDb()
	{

		$this->bootstrap('session');
		Zend_Session::start();
	}
	
}

